clear all
set more off
use Merged_Working_Data

foreach var of varlist EPU* {
	gen l`var' = ln(`var'+1)
}
gen asinh_EPU_National = asinh(EPU_National)
gen asinh_EPU_State = asinh(EPU_State)

foreach var of varlist pres_general_election gov_general_election {
	replace `var' = 0 if `var'==.
}

forvalues x = 1/6 {
	replace gov_general_vote_share = f`x'.gov_general_vote_share if f`x'.gov_general_vote_share!=. & f`x'.gov_general_election==1
	replace gov_general_vote_share = l`x'.gov_general_vote_share if l`x'.gov_general_vote_share!=. & l`x'.gov_general_election==1
	replace pres_vote_margin = f`x'.pres_vote_margin if f`x'.pres_vote_margin!=. & f`x'.pres_general_election==1
	replace pres_vote_margin = l`x'.pres_vote_margin if l`x'.pres_vote_margin!=. & l`x'.pres_general_election==1
}

foreach var of varlist total_new* {
	replace `var' = `var'/population
}

gen ltotal_new_death  = ln(total_new_death + 1/population)
gen ltotal_new_case  = ln(total_new_case + 1/population)

gen pres = pres_general_election==1 | f.pres_general_election==1 | l.pres_general_election==1
gen gov = gov_general_election==1 | f.gov_general_election==1 
gen gov_general_elec_close = gov*(gov_general_vote_share<.52)
gen pres_general_elec_close = pres*(pres_vote_margin<.04)

***Make combined policy index
gen combined_sip = is_sip
replace combined_sip = stayathomeorder if combined_sip==.
gen combined_school = is_school
replace combined_school = schoolclosures if combined_school==.
gen combined_bus = is_bus
replace combined_bus = nonessentialbusinessclosures if combined_bus==.
gen combined_res = is_res
replace combined_res = restaurantlimits if combined_res==.

/*
Let SIPO = 1 when a shelter-in-place order (also known as stay-at-home order) is in effect, 0 otherwise. Define NEBCO, RCO, and SCO analogously.
NEBCOs encompass RCOs. So, we should think of an RCO as NEBCO light.
SIPOs encompass RCOs and SCOs and effectively encompass NEBCOs as well.
These observations lead to the following formula for the stringency of government lockdown orders:
Stringency Index = Max{SIPO, 0.75 * NEBCO + 0.25*SCO, 0.25*RCO + 0.25*SCO}
*/

gen temp = 0.75*combined_bus + 0.25*combined_school
gen temp2 =  0.25*combined_res + 0.25*combined_school
egen combined_covid_index = rowmax(combined_sip temp temp2)
drop temp*

gen indic = 1
replace total_new_case = 0 if total_new_case==.
replace total_new_death = 0 if total_new_death==.
replace combined_covid_index = 0 if combined_covid_index==.

*******Make Ratio and other vars
gen ratio_dhs = ((EPU_National-EPU_State)/((EPU_National+EPU_State)/2))
gen ratio = EPU_State/EPU_National
replace ratio = . if ratio>10 | ratio<.1

gen d_coincident_indic = d.coincident_indic
gen d_unemp = d.unemp
gen dlEPU_State = d.lEPU_State
gen dlEPU_National = d.lEPU_National
gen dEPU_State = d.EPU_State
gen dEPU_National = d.EPU_National
gen l_dlEPU_State = l.dlEPU_State
gen l_dEPU_State = l.dEPU_State
gen l_lEPU_State = l.lEPU_State
gen l_dlEPU_National = l.dlEPU_National
gen l_dEPU_National = l.dEPU_National
gen l_lEPU_National = l.lEPU_National
gen l_EPU_National = l.EPU_National
gen l_EPU_State = l.EPU_State
gen l_total_new_death = l.total_new_death


egen avg_EPU_State = mean(EPU_State), by(state)
egen avg_EPU_National = mean(EPU_National), by(state)

***Data for Graphs
foreach var of varlist EPU_National EPU_State {
	egen state_avg_`var' = mean(`var') if year<2020|(year==2020 & month<3), by(state)
	egen state_avg_2020_`var' = mean(`var') if (year==2021)|(year==2020 & month>2), by(state)
}

gen state_avg_ratio = state_avg_EPU_State/state_avg_EPU_National
gen state_avg_ratio_2020 = state_avg_2020_EPU_State/state_avg_2020_EPU_National


*******************************************
****Long Difference construction
gen quarter = floor((month-1)/3)+1
tsset state_code period

egen temp = mean(EPU_Composite) if year==2019, by(state)
egen EPU_2019 = mean(temp), by(state)
egen temp1 = mean(EPU_Composite) if year==2020 & (month>=4 & month<=6), by(state)
egen EPU_aprjun2020 = mean(temp1), by(state)
egen temp2 = mean(EPU_Composite) if year==2020 & (month>=7 & month<=9), by(state)
egen EPU_julsept2020 = mean(temp2), by(state)
egen temp3 = mean(EPU_Composite) if year==2020 & (month>=10 & month<=12), by(state)
egen EPU_octdec2020 = mean(temp3), by(state)
egen temp4 = mean(EPU_Composite) if year==2021 & (month>=1 & month<=3), by(state)
egen EPU_janmar2021 = mean(temp4), by(state)
egen temp5 = mean(EPU_Composite) if year==2021 & (month>=4 & month<=6), by(state)
egen EPU_aprjun2021 = mean(temp5), by(state)
drop temp*

egen temp6 = mean(unemp) if year==2019, by(state)
egen unemp_2019 = mean(temp6), by(state)
egen temp7 = mean(unemp) if year==2020 & (month>=4 & month<=6), by(state)
egen unemp_aprjun2020 = mean(temp7), by(state)
egen temp8 = mean(unemp) if year==2020 & (month>=7 & month<=9), by(state)
egen unemp_julsept2020 = mean(temp8), by(state)
egen temp9 = mean(unemp) if year==2020 & (month>=10 & month<=12), by(state)
egen unemp_octdec2020 = mean(temp9), by(state)
egen temp10 = mean(unemp) if year==2021 & (month>=1 & month<=3), by(state)
egen unemp_janmar2021 = mean(temp10), by(state)
egen temp11 = mean(unemp) if year==2021 & (month>=4 & month<=6), by(state)
egen unemp_aprjun2021 = mean(temp11), by(state)
drop temp*

gen lEPU_2019 = ln(EPU_2019)
gen lEPU_aprjun2020 = ln(EPU_aprjun2020)
gen lEPU_julsept2020 = ln(EPU_julsept2020)
gen lEPU_octdec2020 = ln(EPU_octdec2020)
gen lEPU_janmar2021 = ln(EPU_janmar2021)
gen lEPU_aprjun2021 = ln(EPU_aprjun2021)

gen diff_unemp_aprjun2020_2019 = unemp_aprjun2020 - unemp_2019
gen diff_unemp_julsept2020_2019 = unemp_julsept2020 - unemp_2019
gen diff_unemp_octdec2020_2019 = unemp_octdec2020 - unemp_2019
gen diff_unemp_janmar2021_2019 = unemp_janmar2021 - unemp_2019
gen diff_unemp_aprjun2021_2019 = unemp_aprjun2021 - unemp_2019
gen diff_aprjun2020_2019 = lEPU_aprjun2020 - lEPU_2019
gen diff_julsept2020_2019 = lEPU_julsept2020 - lEPU_2019
gen diff_octdec2020_2019 = lEPU_octdec2020 - lEPU_2019
gen diff_janmar2021_2019 = lEPU_janmar2021 - lEPU_2019
gen diff_aprjun2021_2019 = lEPU_aprjun2021 - lEPU_2019

egen q_total_new_death = mean(total_new_death), by(quarter year state)
egen q_combined_covid_index = mean(combined_covid_index), by(quarter year state)
egen q_unemp = mean(unemp), by(quarter year state)

***Label vars
label var pres_general_elec_close "Close Presidential Election"
label var gov_general_elec_close "Close Gubernatorial Election"
label var pres "Presidential Election"
label var gov "Gubernatorial Election"
label var coin "Coincident Indicator"
label var unemp "Unemployment Rate"
label var d_coincident_indic "$\Delta$ Coincident Indicator"
label var d_unemp "$\Delta$  Unemployment Rate"
label var l_lEPU_State "Lagged ln(EPU-S)"
label var l_EPU_State "Lagged EPU-S"
label var l_dlEPU_State "Lagged $ \Delta ln(EPU-S) $"
label var l_lEPU_National "Lagged ln(EPU-N)"
label var l_EPU_National "Lagged EPU-N"
label var l_dlEPU_National "Lagged $ \Delta ln(EPU-N) $"
label var total_new_death "New COVID Deaths"
label var total_new_case "New COVID Cases"
label var q_total_new_death "New COVID Deaths"
label var q_unemp "Unemployment"
label var combined_covid_index "Lockdown Stringency Index"
label var q_combined_covid_index "Lockdown Stringency Index"
label var ym "Period"
label var diff_aprjun2020_2019 "$ \Delta $ ln(EPU-C) 2020Q2"
label var diff_julsept2020_2019 "$ \Delta $ ln(EPU-C) 2020Q3"
label var diff_octdec2020_2019 "$ \Delta $ ln(EPU-C) 2020Q4"
label var diff_janmar2021_2019 "$ \Delta $ ln(EPU-C) 2021Q1"
label var diff_aprjun2021_2019 "$ \Delta $ ln(EPU-C) 2021Q2"

STOP STOP STOP
************************************************************************************************************
************************************************************************************************************
***Data posted on website
keep state year month EPU_National EPU_State EPU_Composite
compress
export excel using "State_Policy_Uncertainty.xlsx", firstrow(variables) replace

***************************************************************************************
***Figure 3 - Elections Timeline Figure
cap gen n = [_n]-13
replace n = . if n>12
cap gen pres = .
cap gen gov = .
cap gen se = .

global lags = 12

reghdfe ratio l(-$lags/$lags).pres_general_election unemp  coinc [aweight=population] , ab(state_code year)
forvalues x = 1/$lags {
	replace pres = _b[pres_general_election] if n==0
	replace pres = _b[l`x'.pres_general_election] if n==`x'
	replace pres = _b[f`x'.pres_general_election] if n==-`x'
	replace se = _se[pres_general_election] if n==0
	replace se = _se[l`x'.pres_general_election] if n==`x'
	replace se = _se[f`x'.pres_general_election] if n==-`x'
}
gen se1pres=pres+1.96*se
gen se2pres=pres-1.96*se

reghdfe ratio l(-$lags/$lags).gov_general_election unemp  coinc [aweight=population] , ab(state_code year month)
forvalues x = 1/$lags {
	replace gov = _b[gov_general_election] if n==0
	replace gov = _b[l`x'.gov_general_election] if n==`x'
	replace gov = _b[f`x'.gov_general_election] if n==-`x'
	replace se = _se[gov_general_election] if n==0
	replace se = _se[l`x'.gov_general_election] if n==`x'
	replace se = _se[f`x'.gov_general_election] if n==-`x'
}
gen se1gov=gov+1.96*se
gen se2gov=gov-1.96*se

label var n "Months to Election"
label var pres "Presidential Election"
label var gov "Gubernatorial Election"
twoway (line pres n, lcolor(black))(line se1pres  n, lwidth(.1) lpattern(dash) lcolor(black))(line se2pres  n, lwidth(.1)  lpattern(dash) lcolor(black))(line gov n, lcolor(orange))(line se1gov  n, lwidth(.1)  lpattern(dash) lcolor(orange))(line se2gov  n, lwidth(.1)  lpattern(dash) lcolor(orange)) if abs(n)<11, legend(order(1 4))
graph export "Results\Elections_Timeline_period.png", replace


***************************************************************************************
***Figure A-4
graph dot state_avg_ratio state_avg_ratio_2020, yline(0.7, lcolor(blue)) yline(1.09, lcolor(red)) over(state, label(labsize(tiny))  sort(state_avg_ratio)) legend(label( 1 "Pre-COVID Ratio          " ) label( 2 "Post-COVID Ratio         " ) rows(2) size(vsmall))
graph export "Results\State_National_EPU_Ratios.png", replace height(1200) width(1200)

***************************************************************************************
****Figure A-11
sc diff_aprjun2020_2019  q_combined_covid_index if year==2020 & month==5 
binscatter diff_aprjun2020_2019  q_combined_covid_index if year==2020 & month==5 , xlabel(0(.25)1,labsize(medlarge)) ylabel(0(0.4)2,labsize(medlarge)) nquantiles(10) xtitle(Lockdown Stringency Index, size(large)) ytitle(Change in ln(EPU-C), size(large)) 
graph export "Results\EPUC_and_PolicyIndex_q2_2020.png", replace

sc diff_julsept2020_2019  q_combined_covid_index if year==2020 & month==8 
binscatter diff_julsept2020_2019  q_combined_covid_index if year==2020 & month==8  , xlabel(0(.25)1,labsize(medlarge)) ylabel(0(0.4)2,labsize(medlarge)) nquantiles(10) xtitle(Lockdown Stringency Index, size(large)) ytitle(Change in ln(EPU-C), size(large))
graph export "Results\EPUC_and_PolicyIndex_q3_2020.png", replace

sc diff_octdec2020_2019  q_combined_covid_index if year==2020 & month==11 
binscatter diff_octdec2020_2019  q_combined_covid_index if year==2020 & month==11  , xlabel(0(.25)1,labsize(medlarge)) ylabel(0(0.4)2,labsize(medlarge)) nquantiles(10) xtitle(Lockdown Stringency Index, size(large)) ytitle(Change in ln(EPU-C), size(large))
graph export "Results\EPUC_and_PolicyIndex_q4_2020.png", replace

sc diff_janmar2021_2019  q_combined_covid_index if year==2021 & month==2
binscatter diff_janmar2021_2019  q_combined_covid_index if year==2021 & month==2  , xlabel(0(.25)1,labsize(medlarge)) ylabel(0(0.4)2,labsize(medlarge)) nquantiles(10) xtitle(Lockdown Stringency Index, size(large)) ytitle(Change in ln(EPU-C), size(large))
graph export "Results\EPUC_and_PolicyIndex_q1_2021.png", replace

sc diff_aprjun2021_2019  q_combined_covid_index if year==2021 & month==5
binscatter diff_aprjun2021_2019  q_combined_covid_index if year==2021 & month==5  , xlabel(0(.25)1,labsize(medlarge)) ylabel(0(0.4)2,labsize(medlarge)) nquantiles(10) xtitle(Lockdown Stringency Index, size(large)) ytitle(Change in ln(EPU-C), size(large))
graph export "Results\EPUC_and_PolicyIndex_q2_2021.png", replace


***************************************************************************************
***Table 1: Elections
reghdfe asinh_EPU_National pres gov unemp coinc [aweight=population] if ratio_dhs!=., ab(state_code) vce(robust)
outreg2 using "Results\Elections",  tex(landscape pr frag) drop(unemp coinc) label addtext(Economic Controls, YES, State FE, YES, Year and Month FE, NO) ctitle("ln(EPU-N)") nocons nonotes replace

reghdfe asinh_EPU_National pres gov unemp coinc [aweight=population] if ratio_dhs!=., ab(state_code year month) vce(robust)
outreg2 using "Results\Elections",  tex(landscape pr frag) drop(unemp coinc) label addtext(Economic Controls, YES, State FE, YES, Year and Month FE, YES) ctitle("ln(EPU-N)") nocons nonotes

reghdfe asinh_EPU_National pres gov pres_general_elec_close gov_general_elec_close unemp coinc [aweight=population] if ratio_dhs!=., ab(state_code year month) vce(robust)
outreg2 using "Results\Elections",  tex(landscape pr frag) drop(unemp coinc) label addtext(Economic Controls, YES, State FE, YES, Year and Month FE, YES) ctitle("ln(EPU-N)") nocons nonotes

reghdfe asinh_EPU_State pres gov unemp coinc [aweight=population] if ratio_dhs!=., ab(state_code) vce(robust)
outreg2 using "Results\Elections",  tex(landscape pr frag) drop(unemp coinc) label addtext(Economic Controls, YES, State FE, YES, Year and Month FE, NO) ctitle("ln(EPU-S)") nocons nonotes 

reghdfe asinh_EPU_State pres gov unemp coinc [aweight=population] if ratio_dhs!=., ab(state_code year month) vce(robust)
outreg2 using "Results\Elections",  tex(landscape pr frag) drop(unemp coinc) label addtext(Economic Controls, YES, State FE, YES, Year and Month FE, YES) ctitle("ln(EPU-S)") nocons nonotes

reghdfe asinh_EPU_State pres gov pres_general_elec_close gov_general_elec_close unemp coinc [aweight=population] if ratio_dhs!=., ab(state_code year month) vce(robust)
outreg2 using "Results\Elections",  tex(landscape pr frag) drop(unemp coinc) label addtext(Economic Controls, YES, State FE, YES, Year and Month FE, YES) ctitle("ln(EPU-S)") nocons nonotes

***************************************************************************************
***Table 2
reg diff_aprjun2020_2019  q_combined_covid_index q_total_new_death if year==2020 & month==5 , rob
outreg2 using "Results\EPU_Long_Diffs",  tex(landscape pr frag)  label  ctitle("$\Delta$ EPU 2020Q2") nocons nonotes replace
reg diff_julsept2020_2019  q_combined_covid_index q_total_new_death if year==2020 & month==8  , rob
outreg2 using "Results\EPU_Long_Diffs",  tex(landscape pr frag)  label ctitle("$\Delta$ EPU 2020Q3") nocons nonotes 
reg diff_octdec2020_2019  q_combined_covid_index q_total_new_death if year==2020 & month==11  , rob
outreg2 using "Results\EPU_Long_Diffs",  tex(landscape pr frag)  label  ctitle("$\Delta$ EPU 2020Q4") nocons nonotes 
reg diff_janmar2021_2019  q_combined_covid_index q_total_new_death if year==2021 & month==2  , rob
outreg2 using "Results\EPU_Long_Diffs",  tex(landscape pr frag)  label  ctitle("$\Delta$ EPU 2021Q1") nocons nonotes 
reg diff_aprjun2021_2019  q_combined_covid_index q_total_new_death if year==2021 & month==5  , rob
outreg2 using "Results\EPU_Long_Diffs",  tex(landscape pr frag)  label ctitle("$\Delta$ EPU 2021Q2") nocons nonotes 

***************************************************************************************
***Table 3
reg diff_unemp_aprjun2020_2019  diff_aprjun2020_2019  q_combined_covid_index q_total_new_death if year==2020 & month==5, rob
outreg2 using "Results\Unemp_Long_Diffs",  tex(landscape pr frag)  label  ctitle("$\Delta$ Unemp 2020Q2") nocons nonotes replace
reg diff_unemp_julsept2020_2019  diff_julsept2020_2019  q_combined_covid_index q_total_new_death if year==2020 & month==8, rob
outreg2 using "Results\Unemp_Long_Diffs",  tex(landscape pr frag)  label  ctitle("$\Delta$ Unemp 2020Q3") nocons nonotes
reg diff_unemp_octdec2020_2019  diff_octdec2020_2019  q_combined_covid_index q_total_new_death if year==2020 & month==11, rob
outreg2 using "Results\Unemp_Long_Diffs",  tex(landscape pr frag)  label  ctitle("$\Delta$ Unemp 2020Q4") nocons nonotes
reg diff_unemp_janmar2021_2019  diff_janmar2021_2019  q_combined_covid_index q_total_new_death if year==2021 & month==2, rob
outreg2 using "Results\Unemp_Long_Diffs",  tex(landscape pr frag)  label  ctitle("$\Delta$ Unemp 2021Q1") nocons nonotes
reg diff_unemp_aprjun2021_2019  diff_aprjun2021_2019  q_combined_covid_index q_total_new_death if year==2021 & month==5, rob
outreg2 using "Results\Unemp_Long_Diffs",  tex(landscape pr frag)  label  ctitle("$\Delta$ Unemp 2021Q2") nocons nonotes
